package com.clinic.accessor.db;

import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Component;

import com.clinic.entity.db.Drug;
import com.clinic.entity.param.Index;

@Component
public class DrugAccessor extends AbstractDbAccessor<Drug> {

	public Drug getDrugById(long id) {
		DetachedCriteria criteria = DetachedCriteria.forClass(Drug.class);
		criteria.add(Restrictions.eq(Drug.Columns.DRUG_ID, id));
		return uniqueGeneric(criteria);
	}
	
	public Drug getDrugByName(String name) {
		DetachedCriteria criteria = DetachedCriteria.forClass(Drug.class);
		criteria.add(Restrictions.eq(Drug.Columns.NAME, name));
		return uniqueGeneric(criteria);
	}
	
	public List<Drug> getOrderedDrugsByTerm(String term, String column, Index index) {
		DetachedCriteria criteria = getCriteriaOfOrderedDrugsByTerm(term, column);
		return listGeneric(criteria, index);
	}
	
	public int countOfOrderedDrugsByTerm(String term, String column) {
		DetachedCriteria criteria = getCriteriaOfOrderedDrugsByTerm(term, column);
		return getCount(criteria);
	}
	
	public DetachedCriteria getCriteriaOfOrderedDrugsByTerm(String term, String column) {
		DetachedCriteria criteria = DetachedCriteria.forClass(Drug.class);
		criteria.add(Restrictions.ilike(Drug.Columns.NAME, "%" + term + "%"));
		criteria.addOrder(Order.asc(column));
		return criteria;
	}
	
	public List<Drug> getDrugs(Index index) {
		DetachedCriteria criteria = getCriteriaOfDrugs();
		return listGeneric(criteria, index);
	}
	
	public int countOfDrugs() {
		DetachedCriteria criteria = getCriteriaOfDrugs();
		return getCount(criteria);
	}
	
	public DetachedCriteria getCriteriaOfDrugs() {
		DetachedCriteria criteria = DetachedCriteria.forClass(Drug.class);
		return criteria;
	}
}
